<?php
  	require_once('pclzip.lib.php');
	$uploadDir = "./upload";
	
    // See if there's a file parameter in the URL string
    $file = $_GET['file'];
	$toPath = $_GET['toPath'];
    
    if (isset($file)) {
    	//rm_recursive($toPath);
		$archive = new PclZip($file);
		if ($archive->extract(PCLZIP_OPT_PATH, $toPath, PCLZIP_OPT_REPLACE_NEWER, true) == 0) {
			die("Error : ".$archive->errorInfo(true));
		}
      exit;
    }

	function rm_recursive($filepath) {
	    if (is_dir($filepath) && !is_link($filepath)) {
	        if ($dh = opendir($filepath)) {
	            while (($sf = readdir($dh)) !== false) {
	                if ($sf == '.' || $sf == '..') {
	                    continue;
	                }
	                if (!rm_recursive($filepath.'/'.$sf)) {
	                    Echo $filepath.'/'.$sf.' could not be deleted.';
	                    //throw new Exception($filepath.'/'.$sf.' could not be deleted.');
	                }
	            }
	            closedir($dh);
	        }
	        return rmdir($filepath);
	    }
	    return unlink($filepath);
	}
		
	function delDirectory($dirname) {
		if (is_dir($dirname))
			$dir_handle = opendir($dirname);
		if (!$dir_handle)
			return false;
		while($file = readdir($dir_handle)) {
			if ($file != "." && $file != "..") {
				if (!is_dir($dirname."/".$file))
					unlink($dirname."/".$file);
				else
					delDirectory($dirname.'/'.$file);
			}
		}
		closedir($dir_handle);
		echo "rm " . $dirname . "<br />";
		if (rmdir($dirname)) Echo $dirname . " deleted <br />";
		return true;
	}
		
	function displayArchive($fromPath, $pattern,  $toPath) {
		$handler = opendir($fromPath);
		while ($file = readdir($handler)) {
			if ($file != "." && $file != "..") {
				if (preg_match ($pattern, $file) ) {
				    echo '<FORM name=" . $pattern . " action="" method="get">';
					//echo "Extract " . $file . " from " . $fromPath . " to " . $toPath  . "<br />";
					echo '<input style="width:50%" type="input" name="file" value="' . $fromPath . "/" . $file . '" /> <br />';
					echo '<input style="width:50%" type="input" name="toPath" value="' . $toPath . '" /><br />';
				    echo '<input type="submit" />';
				    echo '</FORM>';
				    
					//$archive = new PclZip($fromPath . "/" . $file);
					//if ($archive->extract(PCLZIP_OPT_PATH, $toPath) == 0) {
					//	die("Error : ".$archive->errorInfo(true));
					//}
				}
			}
    	}
	}
	
	function scanArchive($pattern,  $toPath) {
		displayArchive("./upload", '/'.$pattern.'/i',  $toPath);
	}

	
	scanArchive('kaamelot-softwares-site',    '..');
		
	scanArchive('jira-plugin-kaamelot-site',    '../jiraPlugins/jira-plugin-kaamelot-site');
	scanArchive('jira-plugin-kaamelot-parent',  '../jiraPlugins/jira-plugin-kaamelot-parent');
	scanArchive('jira-plugin-kaamelot-core',    '../jiraPlugins/jira-plugin-kaamelot-parent/jira-plugin-kaamelot-core');
	scanArchive('jira-plugin-kaamelot-i18n',    '../jiraPlugins/jira-plugin-kaamelot-parent/jira-plugin-kaamelot-i18n');
	scanArchive('jira-plugin-kaamelot-settings','../jiraPlugins/jira-plugin-kaamelot-parent/jira-plugin-kaamelot-settings');
	  
	scanArchive('jira-plugin-myrddin-site',    '../jiraPlugins/jira-plugin-myrddin-site');
	scanArchive('jira-plugin-myrddin-parent',  '../jiraPlugins/jira-plugin-myrddin-parent');
	scanArchive('jira-plugin-myrddin-core',    '../jiraPlugins/jira-plugin-myrddin-parent/jira-plugin-myrddin-core');
	scanArchive('jira-plugin-myrddin-i18n',    '../jiraPlugins/jira-plugin-myrddin-parent/jira-plugin-myrddin-i18n');
	scanArchive('jira-plugin-myrddin-settings','../jiraPlugins/jira-plugin-myrddin-parent/jira-plugin-myrddin-settings');
	
  
/*  	
	if ($v_folder_handler = @opendir($v_descr['filename'])) {
	}
	
<?php
	//Open Upload directory
	$dir = opendir("upload");
	//List files in SITE JARs
	while (($file = readdir($dir)) !== false)  {
	  echo "filename: " . $file . "<br />";
	}
	closedir($dir);
	

	function find_files($path, $pattern, $callback) {
		$path = rtrim(str_replace("\\", "/", $path), '/') . '/';
		$matches = Array();
		$entries = Array();
		$dir = dir($path);
		while (false !== ($entry = $dir->read())) {
  			$entries[] = $entry;
  		}
  		$dir->close();
  
  		foreach ($entries as $entry) {
  			$fullname = $path . $entry;
  			if ($entry != '.' && $entry != '..' && is_dir($fullname)) {
  				find_files($fullname, $pattern, $callback);
  			} else if (is_file($fullname) && preg_match($pattern, $entry)) {
  				call_user_func($callback, $fullname);
  			}
  		}
	}	
?> 	
*/	
	
/*		
      		if (preg_match ("/-site.zip$/i", $file) || preg_match ("/-site.jar$/i", $file)) {
		$archiveNames = glob($archiveFileFullName);
		echo $archiveNames  . "\n";
		$archiveCount = count($archiveNames);
		while($i<$archiveCount) {
			$archiveName = $archiveNames[$i];
		    echo $archiveName . "\n";
		    $i++;
		}
		foreach (glob($archiveFileFullName) as $archivename) {
			$archive = new PclZip($archivename);
			if ($archive->extract(PCLZIP_OPT_PATH, $targetPath) == 0) {
				die("Error : ".$archive->errorInfo(true));
			}
		}
	rm_recursive('../_extract');
*/		
	
  
?> 